package org.com.app.knock.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.com.app.knock.KnockApplication;
import org.com.app.knock.activity.MenuActivity;
import org.com.app.knock.activity.SearchActivity;
import org.com.app.knock.activity.SendActivity;
import org.com.app.knock.view.FriendItemView;
import org.com.app.knock.view.LoadingView;
import org.json.JSONArray;
import org.json.JSONObject;

import z.lib.base.BaseAdapter;
import z.lib.base.BaseItem;
import z.lib.base.BaseServiceCallBack;
import z.lib.base.BaseUtils;
import z.lib.base.BaseView;
import z.lib.base.CommonAndroid;
import z.lib.base.LogUtils;
import z.lib.base.BaseUtils.API;
import z.lib.base.callback.RestClient.RequestMethod;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.acv.libs.nbase.BaseFragment;
import com.app.knock.db.AvataList;
import com.app.lazyload.ImageLoader2;
import com.knock.app.R;

public class FriendFragment extends BaseFragment implements AnimationListener {
	private static final String TAG = "FriendFragment";
	View views;
	ListView listView;
	private BaseAdapter baseAdapter;
	public static boolean isLoguot = false;
	private LoadingView loading;
	public static ImageLoader2 imageLoader;
	int onload = 0;
	public FriendFragment() {
		super();
	}

	@Override
	public void setUpFragment(View view) {
		this.views = view;
		isLoguot = false;
		this.views = view;
		listView =(ListView) views.findViewById(R.id.listview_friend);
		LayoutInflater inflater = getActivity().getLayoutInflater();
		RelativeLayout listFooterView = (RelativeLayout)inflater.inflate(
		            R.layout.friend_list_footer, null);
		listView.addFooterView(listFooterView);
		CommonAndroid.getView(views, R.id.add_friends).setOnClickListener(this);	
		CommonAndroid.getView(views, R.id.btn_menu).setOnClickListener(this);
		String[] recourseList=getActivity().getResources().getStringArray(R.array.CountryCodes);
		loading = CommonAndroid.getView(views, R.id.loading);
		imageLoader = new ImageLoader2(getActivity());
	}
	
	@Override
	public void onResume(){
		super.onResume();
		LogUtils.e(TAG,"onresume-------------------");
		onload++;
		getFriend();
	}

	private void getFriend() {
		// TODO Auto-generated method stub
		HashMap<String, String> params = new HashMap<String, String>();
		BaseUtils.execute(getActivity(), RequestMethod.POST, API.API_FRIEND, params, callback);
	}
	
	private BaseServiceCallBack callback = new BaseServiceCallBack() {

		public void onStart() {
			if(onload <=1){
				CommonAndroid.showView(true, loading);
			}
			CommonAndroid.hiddenKeyBoard(getActivity());
		};

		public void onError(String message) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				LogUtils.e(TAG,"er==" + message);
			}
		};

		public void onSucces(String response) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				JSONObject mainJsonObject;
				List<BaseItem> baseItems = new ArrayList<BaseItem>();
				baseItems.clear();
				try {
					mainJsonObject = new JSONObject(response);
					JSONArray array = mainJsonObject.getJSONArray("data");
					for(int i =0; i < array.length();i++){
						if(!"null".equals(array.getJSONObject(i).getString("id"))){
							baseItems.add(new BaseItem(array.getJSONObject(i)));
						}
						
					}
//					LogUtils.e(TAG,mainJsonObject.toString());
					loadData(baseItems);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
	};

	private void loadData(List<BaseItem> baseItems) {
		// TODO Auto-generated method stub
		/*for(int i = 0; i < KnockApplication.listFriend.size()recourseList.length; i++){
			String[] temp =recourseList[i].split(",") KnockApplication.listFriend.get(i).split(";");
			LogUtils.e(TAG,"data---"+ KnockApplication.listFriend.get(i) +"==="+  temp[0].trim());
			JSONObject itemObj = new JSONObject();
		    try
		    {
		    	itemObj.put("area_device", temp[0].trim());
		        itemObj.put("objectId_device", temp[1].trim());
		        baseItems.add(new BaseItem(itemObj));
		    }catch(Exception e){
		    }
		    
		}*/
        
        baseAdapter = new BaseAdapter(getActivity(), new ArrayList<Object>()) {
			@Override
			public BaseView getView(Context context, Object data) {
				final FriendItemView phonecodeListItemView = new FriendItemView(context);
				return phonecodeListItemView;
			}
		};
		baseAdapter.clear();
		baseAdapter.addAllItems(baseItems);
		baseAdapter.notifyDataSetChanged();
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				BaseUtils.playMedia(getActivity(),R.raw.se_tap);
				BaseItem baseItem = (BaseItem) parent.getItemAtPosition(position);
				KnockApplication.device_send_username = baseItem.getString("name");
				KnockApplication.device_send_userid = baseItem.getString("id");
				
				String avatar_id 	= baseItem.getString("avatar_id");
				String avatar 		= baseItem.getString("avatar");
				if(!"".equals(avatar)){
					KnockApplication.device_send_avatar = avatar;
				}else if(!"".equals(avatar_id)){
					String avata = new AvataList(getActivity()).getAvata(avatar_id);
					KnockApplication.device_send_avatar = avata;
				}
				
//				startFragment(new SendFragment(), null);
				Intent Page = new Intent(getActivity(), SendActivity.class);
				getActivity().startActivityForResult(Page,0);
			}
		});
		listView.setAdapter(baseAdapter);
	}

	@Override
	public int layoytResurce() {
		return R.layout.friend_list;
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		int idView = v.getId();
		LogUtils.e("kkkkkkkkkk","kkkkkkk1");
		Intent Page;
		switch (idView) {
		case R.id.btn_menu:
			/*new MenuDialog(getActivity(), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {

				}
			}).show();*/
			Page = new Intent(getActivity(), MenuActivity.class);
			getActivity().startActivityForResult(Page,0); 
			break;
		case R.id.add_friends:
//			startFragment(new SearchFragment(), null);
			Page = new Intent(getActivity(), SearchActivity.class);
			getActivity().startActivityForResult(Page,0);
			break;
		default:
			break;
		}
	}

	@Override
	public void onAnimationStart(Animation animation) {

	}

	@Override
	public void onAnimationEnd(Animation animation) {
		mHandle.sendEmptyMessage(0);
	}

	@Override
	public void onAnimationRepeat(Animation animation) {

	}
}